package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.dto.ArticleDto;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.dto.ArticleInfoDto;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.IApArticleContentService;
import com.heima.article.service.IApArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.task.CreateHtmlTask;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author syl
 * @since 2022-11-15
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {

    @Autowired
    private IApArticleContentService apArticleContentService;

    @Autowired
    private CreateHtmlTask createHtmlTask;

    @Override
    public ResponseResult<Long> saveApArticle(ArticleDto dto) throws InvocationTargetException, IllegalAccessException {
        if (dto.getId () == null) {// 新增
            ApArticle apArticle = new ApArticle ();
            BeanUtils.copyProperties (dto , apArticle);
            apArticle.setCreatedTime (new Date ());
            this.save (apArticle);

            ApArticleContent apArticleContent = new ApArticleContent ();
            apArticleContent.setArticleId (apArticle.getId ());
            apArticleContent.setContent (dto.getContent ());

            apArticleContentService.save (apArticleContent);

            //生成动态页面
            createHtmlTask.createHtml (apArticle , dto.getContent ());

            return ResponseResult.okResult (apArticle.getId ());
        } else {  //修改
            ApArticle apArticle = new ApArticle ();
            BeanUtils.copyProperties (dto , apArticle);
            apArticle.setCreatedTime (new Date ());
            this.updateById (apArticle);

            LambdaQueryWrapper<ApArticleContent> queryWrapper = new LambdaQueryWrapper<> ();
            queryWrapper.eq (ApArticleContent::getArticleId , dto.getId ());

            ApArticleContent apArticleContent = apArticleContentService.getOne (queryWrapper);
            if (apArticleContent != null) {
                apArticleContent.setContent (dto.getContent ());
                apArticleContentService.updateById (apArticleContent);

                //生成动态页面
                createHtmlTask.createHtml (apArticle , dto.getContent ());


            }
            return ResponseResult.okResult (apArticle.getId ());
        }
    }

    @Override
    public ResponseResult load(ArticleHomeDto dto , int type) {

        LambdaQueryWrapper<ApArticle> queryWrapper = new LambdaQueryWrapper<> ();
        //条件：频道id为0 代表查询所有的频道
        if (dto.getChannelId () != 0) {
            queryWrapper.eq (ApArticle::getChannelId , dto.getChannelId ());
        }
        //隐藏条件：未删除且未下架的文章  根据发布时间排序
        queryWrapper.eq (ApArticle::getIsDelete , false); //未删除
        queryWrapper.eq (ApArticle::getIsDown , false); //未下架
        queryWrapper.orderByDesc (ApArticle::getPublishTime);

        if (type == 0) {
            queryWrapper.gt (ApArticle::getPublishTime , dto.getMaxTime ());
        } else {
            queryWrapper.gt (ApArticle::getPublishTime , dto.getMinTime ());
        }


        //分页 目前只需要每页显示条数
        Page<ApArticle> page = new Page<> (1 , dto.getSize ());

        Page<ApArticle> page1 = this.page (page , queryWrapper);

        return ResponseResult.okResult (page1.getRecords ());
    }

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        Long articleId = dto.getArticleId ();
        ApArticle apArticle = this.getById (articleId);
        if (apArticle == null) {
            return ResponseResult.errorResult (AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        LambdaQueryWrapper<ApArticleContent> queryWrapper = new LambdaQueryWrapper<> ();
        queryWrapper.eq (ApArticleContent::getArticleId , articleId);
        ApArticleContent one = apArticleContentService.getOne (queryWrapper);

        Map map = new HashMap ();
        map.put ("config" , apArticle);
        map.put ("content" , one);
        return ResponseResult.okResult (map);
    }
}
